<template>
  <div>
    <el-row>
      <el-col :span="12" class="card wide-label">
        <el-card header="Cpu信息">
          <div class="item">
            <label>核心数</label>
            <span>{{ data.cpu.num }}</span>
          </div>
          <div class="item">
            <label>用户使用率</label>
            <span>{{ data.cpu.used }}%</span>
          </div>
          <div class="item">
            <label>系统使用率</label>
            <span>{{ data.cpu.sys }}%</span>
          </div>
          <div class="item">
            <label>空闲率</label>
            <span>{{ data.cpu.free }}%</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12" class="card">
        <el-card header="内存信息">
          <div class="item">
            <label>总内存</label>
            <span>{{ data.mem.total }} GB</span>
          </div>
          <div class="item">
            <label>使用内存</label>
            <span>{{ data.mem.used }} GB</span>
          </div>
          <div class="item">
            <label>剩余内存</label>
            <span>{{ data.mem.free }} GB</span>
          </div>
          <div class="item">
            <label>使用率</label>
            <span>{{ data.mem.usage }}%</span>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12" class="card wide-label">
        <el-card header="服务器信息">
          <div class="item">
            <label>服务器名称</label>
            <span>{{ data.sys.computerName }}</span>
          </div>
          <div class="item">
            <label>服务器IP</label>
            <span>{{ data.sys.computerIp }}</span>
          </div>
          <div class="item">
            <label>操作系统</label>
            <span>{{ data.sys.osName }}</span>
          </div>
          <div class="item">
            <label>系统架构</label>
            <span>{{ data.sys.osArch }}</span>
          </div>
        </el-card>
      </el-col>
      <el-col :span="12" class="card">
        <el-card header="Java虚拟机信息">
          <!-- <div class="item">
            <label>Java名称</label>
            <span>{{ data.jvm.name }}</span>
          </div> -->
          <div class="item">
            <label>启动时间</label>
            <span>{{ data.jvm.startTime }}</span>
          </div>
          <div class="item">
            <label>运行时间</label>
            <span>{{ data.jvm.runTime }}</span>
          </div>
          <div class="item">
            <label>Java版本</label>
            <span>{{ data.jvm.version }}</span>
          </div>
          <div class="item">
            <label>安装路径</label>
            <span>{{ data.jvm.home }}</span>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24" class="card">
        <el-card header="磁盘信息">
          <el-table :data="data.sysFiles" style="width: 100%">
            <el-table-column prop="dirName" label="目录" />
            <el-table-column prop="total" label="总容量" />
            <el-table-column prop="used" label="已使用" />
            <el-table-column prop="free" label="剩余" />
            <el-table-column prop="usage" label="使用率" :formatter="formatter" />
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, onMounted } from 'vue'
import { ElCard, ElTable, ElTableColumn } from 'element-plus'
import { monitorApi } from '@/api/modules/monitor/monitor'

const data = reactive({ cpu: {} as any, mem: {} as any, jvm: {} as any, sys: {} as any, sysFiles: [] as any[] })
onMounted(() => {
  monitorApi.findServer().then(resp => {
    if (resp.success) {
      Object.assign(data, resp.data)
    }
  })
})
const formatter = (row: any, column: any, cellValue: any) => {
  if (column.property === 'usage') {
    return `${cellValue}%`
  } else {
    return cellValue
  }
}
</script>

<style lang="scss" scoped>
.card {
  padding: 0 10px;
  margin-bottom: 15px;
  .item {
    padding: 3px 0;
    label {
      display: inline-block;
      min-width: 70px;
      &:after {
        content: ':';
      }
    }
    span {
      color: $--color-text-secondary;
    }
  }
  &.wide-label {
    .item {
      label {
        min-width: 85px;
      }
    }
  }
}
</style>
